﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Security;

    internal class DeleteFolderCommandHandler : XmlCommandHandlerBase
    {
        protected override void BuildXml()
        {
            if (!base.CurrentFolder.CheckAcl(AccessControlRules.FolderDelete))
            {
                ConnectorException.Throw(0x67);
            }
            if (base.CurrentFolder.ClientPath == "/")
            {
                ConnectorException.Throw(0x6d);
            }
            else
            {
                if (!Directory.Exists(base.CurrentFolder.ServerPath))
                {
                    ConnectorException.Throw(0x74);
                }
                try
                {
                    Directory.Delete(base.CurrentFolder.ServerPath, true);
                }
                catch (UnauthorizedAccessException)
                {
                    ConnectorException.Throw(0x68);
                }
                catch (SecurityException)
                {
                    ConnectorException.Throw(0x68);
                }
                catch (ArgumentException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (PathTooLongException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (Exception)
                {
                    ConnectorException.Throw(110);
                }
                try
                {
                    Directory.Delete(base.CurrentFolder.ThumbsServerPath, true);
                }
                catch
                {
                }
            }
        }
    }
}

